<?php

!defined('IN_APP') && exit('Access Denied');

class customermodel {

    var $db;
    var $base;

    function __construct(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    function get($cid) {
        $customer = $this->db->fetch_first("SELECT * FROM " . DB_TABLEPRE . "customer WHERE cid='$cid'");
        $customer['processingdate'] = tdate($customer['processingtime']);
        return $customer;
    }

    function getpassport($cid) {
        $passport = $this->db->fetch_first("SELECT * FROM " . DB_TABLEPRE . "passport WHERE cid=$cid");
        $passport['passportstart'] = tdate($passport['passportstart'], 1);
        $passport['passportend'] = tdate($passport['passportend'], 1);
        $passport['departuretime'] = tdate($passport['departuretime'], 1);
        return $passport;
    }

    function getpayment($cid) {
        $payment = $this->db->fetch_first("SELECT * FROM " . DB_TABLEPRE . "payment WHERE cid=$cid");
        return $payment;
    }

    function add($username, $gender, $mobile='', $email='', $phone='', $fax='', $address='', $postcode='', $state=0, $country=0, $passporttype=1, $inviteletter=0, $peoplenum=1, $urgent=0, $interview=0, $price=0, $sourcetype=1, $status=0, $signedstate=0) {
        $this->db->query("INSERT INTO " . DB_TABLEPRE . "customer(username,gender,mobile,email,phone,fax,address,postcode,state,country,passporttype,inviteletter,peoplenum,urgent,interview,price,processingtime,processingarea,operator,sourcetype,status,signedstate,time) VALUES "
                . "('$username',$gender,'$mobile','$email','$phone','$fax','$address','$postcode',$state,$country,$passporttype,$inviteletter,$peoplenum,$urgent,$interview,$price,{$this->base->time},{$this->base->user['areaid']},'" . $this->base->user['username'] . "',$sourcetype,$status,$signedstate,{$this->base->time})");
        return $this->db->insert_id();
    }

    function addpassport($cid, $passportnum, $passportstart, $passportend, $username, $gender, $homecity, $passportcity, $departuretime, $comment) {
        $this->db->query("INSERT INTO " . DB_TABLEPRE . "passport(cid,passportnum,passportstart,passportend,username,gender,homecity,passportcity,departuretime,comment,time) VALUES (" .
                "$cid,'$passportnum',$passportstart,$passportend,'$username',$gender,$homecity,$passportcity,$departuretime,'$comment',{$this->base->time})");
        return $this->db->insert_id();
    }

    function addpayment($cid, $marketprice, $actualprice, $passportexpense, $payobject, $insuranceexpense, $invoice, $invoicemoney, $money, $paytype, $finalpay, $finalpaystatus, $confirmor) {
        $this->db->query("INSERT INTO " . DB_TABLEPRE . "payment(cid,marketprice,actualprice,passportexpense,payobject,insuranceexpense,invoice,invoicemoney,money,paytype,finalpay,finalpaystatus,confirmor) VALUES ($cid,$marketprice,$actualprice,$passportexpense,$payobject,$insuranceexpense,$invoice,$invoicemoney,$money,$paytype,$finalpay,$finalpaystatus,'$confirmor')");
        return $this->db->insert_id();
    }

    function update($cid, $username, $gender, $mobile='', $email='', $phone='', $fax='', $address='', $postcode='', $state=0, $country=0, $passporttype=1, $inviteletter=0, $peoplenum=1, $urgent=0, $interview=0, $price=0, $sourcetype=1, $status=0, $signedstate=0) {
        $this->db->query("UPDATE " . DB_TABLEPRE . "customer SET username='$username',gender=$gender,mobile='$mobile',email='$email',phone='$phone',fax='$fax',address='$address',postcode='$postcode',state=$state,country=$country,passporttype=$passporttype,inviteletter=$inviteletter,peoplenum=$peoplenum,urgent=$urgent,interview=$interview,price=$price,sourcetype=$sourcetype,status=$status,signedstate=$signedstate WHERE cid=$cid");
    }

    function updatepassport($cid, $passportnum, $passportstart, $passportend, $username, $gender, $homecity, $passportcity, $departuretime, $comment) {
        $this->db->query("UPDATE " . DB_TABLEPRE . "passport SET passportnum='$passportnum',passportstart=$passportstart,passportend=$passportend,username='$username',gender=$gender,homecity=$homecity,passportcity=$passportcity,departuretime=$departuretime,comment='$comment' WHERE cid=$cid");
    }

    function updatepayment($cid, $marketprice, $actualprice, $passportexpense, $payobject, $insuranceexpense, $invoice, $invoicemoney, $money, $paytype, $finalpay, $finalpaystatus, $confirmor) {
        $this->db->query("UPDATE " . DB_TABLEPRE . "payment SET marketprice=$marketprice,actualprice=$actualprice,passportexpense=$passportexpense,payobject=$payobject,insuranceexpense=$insuranceexpense,invoice=$invoice,invoicemoney=$invoicemoney,money=$money,paytype=$paytype,finalpay=$finalpay,finalpaystatus=$finalpaystatus,confirmor='$confirmor' WHERE cid=$cid");
    }

    /* 根据时间获取客户信息 */

    function get_by_time($timestart, $timeend, $start=0, $limit=50) {
        $customerlist = array();
        $query = $this->db->query("SELECT * FROM " . DB_TABLEPRE . "customer WHERE time>$timestart AND time<$timeend ORDER BY time DESC LIMIT $start,$limit");
        while ($customer = $this->db->fetch_array($query)) {
            $customer['processingdate'] = tdate($customer['processingtime']);
            $customer['id'] = sprintf("%06d", $customer['cid']);
            $customer['totalmoney'] = $customer['peoplenum'] * $customer['price'];
            $customerlist[] = $customer;
        }
        return $customerlist;
    }

    /* 根据客户状态查询 */

    function get_by_status($status=0, $timestart=0, $timeend=0, $start=0, $limit=50) {
        $customerlist = array();
        $sql = "SELECT * FROM " . DB_TABLEPRE . "customer WHERE status=$status ";
        $timestart && $sql.= " AND time>$timestart ";
        $timeend && $sql.=" AND time<$timeend ";
        $sql.="ORDER BY time DESC  LIMIT $start,$limit";
        $query = $this->db->query($sql);
        while ($customer = $this->db->fetch_array($query)) {
            $customer['processingdate'] = tdate($customer['processingtime']);
            $customer['id'] = sprintf("%06d", $customer['cid']);
            $customer['totalmoney'] = $customer['peoplenum'] * $customer['price'];
            $customerlist[] = $customer;
        }
        return $customerlist;
    }

    function get_by_operator($operator, $start=0, $limit=50) {
        $customerlist = array();
        $query = $this->db->query("SELECT * FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' ORDER BY time DESC LIMIT $start,$limit");
        while ($customer = $this->db->fetch_array($query)) {
            $customer['processingdate'] = tdate($customer['processingtime']);
            $customer['id'] = sprintf("%06d", $customer['cid']);
            $customer['totalmoney'] = $customer['peoplenum'] * $customer['price'];
            $customerlist[] = $customer;
        }
        return $customerlist;
    }

    function get_sum_status() {
        $statusresult = array();
        $operator = $this->base->user['username'];
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=0 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=1 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=2 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=3 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=4 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=5 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=6 ");
        $statusresult[] = $this->db->result_first("SELECT COUNT(*) AS num FROM " . DB_TABLEPRE . "customer WHERE operator='$operator' AND status=9 ");
        return $statusresult;
    }

    function search($username, $mobile) {
        $customerlist = array();
        $sql = "SELECT * FROM " . DB_TABLEPRE . "customer WHERE 1=1 ";
        $username && $sql.= " AND username LIKE '%$username%' ";
        $mobile && $sql.=" AND mobile LIKE '%$mobile%' ";
        $sql.= "ORDER BY time DESC LIMIT 0,100";
        $query = $this->db->query($sql);
        while ($customer = $this->db->fetch_array($query)) {
            $customer['processingdate'] = tdate($customer['processingtime']);
            $customer['id'] = sprintf("%06d", $customer['cid']);
            $customer['totalmoney'] = $customer['peoplenum'] * $customer['price'];
            $customerlist[] = $customer;
        }
        return $customerlist;
    }

}

?>